{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ce052994",
   "metadata": {},
   "source": [
    "---  \n",
    "* 时间：2022-10-25/week10  \n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c9c6f73",
   "metadata": {},
   "source": [
    "# 上周回顾  \n",
    "\n",
    "> 1. while循环：while True --> 无限循环    \n",
    ">> 1. 指定次数，while True 可以实现for循环的指定次数，增加一个count计数器即可。  \n",
    ">> 2.while True 一般与 用户输入 一起使用，让用户可以重复输入，直到某些条件成立结束（购物车）  \n",
    "> 2.购物车实践（平时作业）  \n",
    "  \n",
    "# 本周内容  \n",
    "> 1. 元组  \n",
    "> 2. 集合  \n",
    "> 3. 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61b175b3",
   "metadata": {},
   "source": [
    "# 1.元组(tuple)  \n",
    "> 1.元组的基本内容（形态、定义）   \n",
    "  \n",
    "> 2.元组的性质  \n",
    ">> 1.有序性（indexing , sliceing）  \n",
    ">> 2.<font color=\"red\">**不可变性** , <font color=\"red\">**元组没有增、删、改**\n",
    "  \n",
    "> 3.元组的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "93d2a3fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(111, 112, 113) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网3_学号 = (111,112,113)  ## 元组用圆括号\n",
    "print(网3_学号,type(网3_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "af4df454",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1111, 1112, 1113) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网2_学号 = 1111,1112,1113  ## 元组不使用括号也可\n",
    "print(网2_学号,type(网2_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c66be2e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=7, tm_sec=28, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组案例 不可变性\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8eb091da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3], [2, 3, 4]) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tuple_test = ([1,2,3],[2,3,4])  # 元组内放列表，列表可变\n",
    "print(tuple_test,type(tuple_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a410aa80",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_test[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "40bea3bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 100], [2, 3, 4])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_test  # 元组内放列表，列表可变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c3fbfa62",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一般情况下，元组里面存放的是 常量（不可变），常量：数字， 字符串  \n",
    "# 不存在变量，如 列表、字典等"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "862df3ee",
   "metadata": {},
   "source": [
    "## 2.集合（sets）  \n",
    "> 1.集合的基本概念  \n",
    ">> 1.性质：没有重复项  \n",
    ">> 2.性质：无序性  \n",
    "\n",
    "> 2.集合的一般方法  \n",
    ">> 1. 集合的运算:& , |  \n",
    ">> 2.集合的一般方法: intersection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "71dbf758",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['aa', 'bb', 'cc', 'aa']\n",
      "{'bb', 'aa', 'cc'}\n"
     ]
    }
   ],
   "source": [
    "a_list = [\"aa\",\"bb\",\"cc\",\"aa\"]\n",
    "a_sets = {\"aa\",\"bb\",\"cc\",\"aa\"}  ## 性质：没有重复项\n",
    "print(a_list)\n",
    "print(a_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bc5c2a1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a组 = {1801,1802,1803}\n",
    "b组 = {1809,1808,1801}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca5e7de3",
   "metadata": {},
   "source": [
    "* 集合的与运算，交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "98543421",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a组 & b组  ## 可筛选出两个集合中的相同项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f147a00e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a组.intersection(b组)  ## 可筛选出两个集合中的相同项"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af38b9db",
   "metadata": {},
   "source": [
    "* 集合的或运算，并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "455a99ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1808, 1809}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a组 | b组  ## 排除相同项，合并两个集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "63ccacf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a组 | b组)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "baab89e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1808, 1809}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a组.union(b组)  ## 排除相同项，合并两个集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80a4dc13",
   "metadata": {},
   "source": [
    "# <font color=\"red\">**3.字典**（dict）  \n",
    "  \n",
    "> 1.字典的基本概念：  \n",
    ">> 1.字典性质：  \n",
    ">>> 1.无序性  \n",
    ">>> 2.keys是不可变的：  \n",
    ">>> 3.keys是唯一的  \n",
    ">> 2. 字典的形态  \n",
    "  \n",
    "> 2.字典的一般方法  \n",
    ">> 1.字典的创建  \n",
    ">> 2.字典的函数和方法  \n",
    ">>> 1.增  \n",
    ">>> 2.删  \n",
    ">>> 3.改\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91f080a9",
   "metadata": {},
   "source": [
    "## 字典的性质和概念"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7b5f372f",
   "metadata": {},
   "outputs": [],
   "source": [
    "c组 = {\n",
    "    211001:\"marry\",\n",
    "    211002:\"susan\",\n",
    "    211003:\"jack\"\n",
    "}  ## 字典写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "5b2b5326",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{211001: 'marry', 211002: 'susan', 211003: 'jack'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(c组,type(c组))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "05a27376",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 因为学号是唯一的，可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "4f6123b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "d组 = {\n",
    "    211001:\"marry\",\n",
    "    211002:\"susan\",\n",
    "    211003:\"jack\",\n",
    "    211004:\"marry\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "87a01930",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{211001: 'marry', 211002: 'susan', 211003: 'jack', 211004: 'marry'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(d组,type(d组))  ## 名字可以重复，但是 学号 作为 key 不能重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "aa35ef42",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的取值：通过keys 进行取值，不像 列表 、元组和字符串通过index取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "43aadf52",
   "metadata": {},
   "outputs": [],
   "source": [
    "d组 = {\n",
    "    211001:[\"marry\",\"广州\",19],  ## 通过keys拉取所有数据\n",
    "    211002:\"susan\",\n",
    "    211003:\"jack\",\n",
    "    211004:\"marry\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c64092b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['marry', '广州', 19]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d组[211001]  ## 通过 keys 拉取所有数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38f3564d",
   "metadata": {},
   "source": [
    "## 字典的一般方法  \n",
    "* 1.按字典的形式直接创建（key : value）  \n",
    "```  \n",
    "d组 = {\n",
    "    211001:\"marry\",\n",
    "    211002:\"susan\",\n",
    "    211003:\"jack\",\n",
    "    211004:\"marry\"\n",
    "}  \n",
    "```\n",
    "* 2.使用 dic() 方法创建  \n",
    "```  \n",
    "e组 = dict(\n",
    "    stu211001=\"marry\",\n",
    "    stu211002=\"susan\"\n",
    ")  \n",
    "```  \n",
    "* 3.# 使用字典增加的方法创建字典  \n",
    "```  \n",
    "d组 = {}\n",
    "d组[\"stu211001\"] = \"marry\"\n",
    "d组[\"stu211002\"] = \"susan\"\n",
    "d组[\"stu211003\"] = \"jack\"\n",
    "d组[\"stu211004\"] = \"marry\"  \n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b85f90fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 dic() 方法创建\n",
    "e组 = dict(\n",
    "    stu211001=\"marry\",\n",
    "    stu211002=\"susan\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "4206921a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方法创建字典 ： dict[key] = value\n",
    "\n",
    "d组 = {}\n",
    "d组[\"stu211001\"] = \"marry\"\n",
    "d组[\"stu211002\"] = \"susan\"\n",
    "d组[\"stu211003\"] = \"jack\"\n",
    "d组[\"stu211004\"] = \"marry\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "afe19354",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu211001': 'marry',\n",
       " 'stu211002': 'susan',\n",
       " 'stu211003': 'jack',\n",
       " 'stu211004': 'marry'}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "861c7c80",
   "metadata": {},
   "outputs": [],
   "source": [
    "e组_keys = [\"211001\",\"211002\",\"211003\",\"211004\"]\n",
    "e组_values = [\"marry\",\"susan\",\"jack\",\"marry\"]\n",
    "for i in range(4):\n",
    "    e组[e组_keys[i]] = e组_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "ce101c59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu211001': ['marry'],\n",
       " 'stu211002': 'susan',\n",
       " '211002': 'susan',\n",
       " '211003': 'jack',\n",
       " '211004': 'marry',\n",
       " '211001': 'marry'}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "11ab7017",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班_keys = [\"stu1800001\",\"stu1800002\",\"stu1800003\",\"stu1800004\"]\n",
    "网新一班_values = ['李一峰','Jack','Marry','周杰伦']\n",
    "网新一班 = {}\n",
    "for i in range(4):\n",
    "    网新一班[网新一班_keys[i]] = 网新一班_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "be43f582",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5410851",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
